﻿using System;
using System.Collections.Concurrent;
using Grundy.Common;
using Grundy.Entity;
using Grundy.Message;

namespace Grundy.Licensing.Server
{
    public class FloatingCountedConcurrentSessionsStrategy : CountedConcurrentSessionsStrategy
    {
        public FloatingCountedConcurrentSessionsStrategy(License license, LicenseEntitlementValidity licenseEntitlementValidity, FeatureIdentificationKey featureIdentificationKey) : base(license, licenseEntitlementValidity, featureIdentificationKey) { }
        
        protected override Guid VerifyValidityAndCheckout(ConcurrentDictionary<Guid, ClientSession> clientSessions, LicenseRequestInformation lri, IdentifierCollection clientIdentifiersToUse)
        {
            var license = (CountedFloatingLicense) License;
            var used = clientSessions.Count;
            if (used >= license.Limit)
                throw new CountedFloatingLicensesExhaustedException(FeatureIdentificationKey, license, used);
            return AddClientSession(lri, clientIdentifiersToUse);
        }
    }
}